#include <stdio.h>
#include <iostream>

using namespace std;

int t[20000];

int find(int x) {
    if (t[x] == 0) {
        return x;
    }
    return find(t[x]);
}

void merge(int a, int b) {
    int q = find(a);
    int p = find(b);
    if (q != p) {
        t[q] = p;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int op, a, b, c;
        scanf("%d", &op);
        if (op == 1) {
            scanf("%d%d", &a, &b);
            merge(a, b);
        } else if (op == 2) {
            scanf("%d%d%d", &a, &b, &c);
            int s = a % c;
            int t = b % c;
            printf("%s\n", (find(s) == find(t) ? "YES" : "NO"));
        }
    }
}